home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / SOURCE.BIN / MovingAnimation.java < prev    next >
Encoding:
Java Source  |  1997-06-19  |  4.7 KB  |  168 lines

  1. package symantec.itools.multimedia;
  2.  
  3.  
  4. import java.awt.Dimension;
  5. import java.awt.Graphics;
  6. import java.awt.Image;
  7.  
  8. //     01/29/97    TWB    Integrated changes from Macintosh
  9.  
  10. /**
  11.  * MovingAnimation component.  Draws a series of images at sequential horizontal
  12.  * positions within the component area.  Each frame is drawn at a given offset
  13.  * from the previous frame.
  14.  *
  15.  * @see Animator
  16.  * @version 1.0, Nov 26, 1996
  17.  * @author Symantec
  18.  */
  19.  
  20.  
  21. public class MovingAnimation
  22.     extends Animator
  23.     implements Runnable
  24. {
  25.     int loopslot;
  26.     int shiftOffset;
  27.     private int curOffset;
  28.  
  29.     /**
  30.      * Constructs a default MovingAnimation.
  31.      */
  32.     public MovingAnimation()
  33.     {
  34.         shiftOffset = 10;
  35.         loopslot    = 0;
  36.         curOffset   = 0;
  37.         forever     = true;
  38.     }
  39.  
  40.     /**
  41.      * Sets the animation shift offset.  Each image is shifted horizontally
  42.      * from the previous image by the specified offset.  Offset may be position
  43.      * or negative. If the offset is positive, the first image is drawn on the
  44.      * left side of the component and subsequent images are drawn to the right
  45.      * of the first image by offset pixels.  If the offset is negative, the
  46.      * first image is drawn at the right side of the component area.
  47.      * @param offset shift offset amount, in pixels
  48.      * @see #getShiftOffset
  49.      */
  50.     public void setShiftOffset(int offset)
  51.     {
  52.         shiftOffset = offset;
  53.     }
  54.  
  55.     /**
  56.      * Gets the animation shift offset.
  57.      * @return int current shift offset amount, in pixels
  58.      * @see #setShiftOffset
  59.      */
  60.     public int getShiftOffset()
  61.     {
  62.         return shiftOffset;
  63.     }
  64.  
  65.     /**
  66.      * Body of MovingAnimation Thread.  This method is called by the Java Virtual Machine
  67.      * in response to a call to the start method of this object.
  68.      */
  69.  
  70.     public void run()
  71.     {
  72.         Thread.currentThread().setPriority(Thread.NORM_PRIORITY - 1);
  73.  
  74.         Dimension d = size();
  75.         int imageCount = images.size();
  76.  
  77.         if (imageCount > 1)
  78.         {
  79.             if (shiftOffset < 0)
  80.                 curOffset = d.width - maxWidth;
  81.  
  82.             int count = 0;
  83.  
  84.             while (true)
  85.             {
  86.                 d = size();        //Needed if the component resizes while running.
  87.  
  88.                 if (++loopslot >= imageCount)
  89.                 {
  90.                     if (++count > numLoops && !forever)
  91.                         break;
  92.  
  93.                     loopslot = 0;
  94.                     curOffset += shiftOffset;
  95.  
  96.                     if (curOffset < 0)
  97.                         curOffset = d.width - maxWidth;
  98.  
  99.                     else if (curOffset + maxWidth > d.width)
  100.                         curOffset = 0;
  101.                 }
  102.  
  103.                 repaint();
  104.  
  105.                 try
  106.                 {
  107.                     Thread.sleep(delay);
  108.                 }
  109.                 catch (InterruptedException e)
  110.                 {
  111.                     break;
  112.                 }
  113.             }
  114.         }
  115.     }
  116.  
  117.     /**
  118.      * Incrementally updates an image as image data becomes available.
  119.      * This is a standard Java AWT method which gets called by the AWT to
  120.      * incrementally draw an image as more image data becomes available.
  121.      * @param img the image being drawn
  122.      * @param flags image update flags (see class ImageObserver)
  123.      * @param x horizontal position
  124.      * @param y vertical position
  125.      * @param w width
  126.      * @param h height
  127.      *
  128.      * @return true if image has been completely loaded
  129.      *
  130.      * @see java.awt.image.ImageObserver
  131.      * @see java.awt.image.ImageObserver#imageUpdate
  132.      */
  133.     public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h)
  134.     {
  135.         if ((flags & (SOMEBITS|FRAMEBITS|ALLBITS)) != 0)
  136.             if ((images != null) && (loopslot < images.size()) && (((AnimatorImage)images.elementAt(loopslot)).image == img))
  137.                 repaint(100);
  138.  
  139.         return (flags & (ALLBITS|ERROR)) == 0;
  140.     }
  141.  
  142.     /**
  143.      * Paints this component using the given graphics context.
  144.      * This is a standard Java AWT method which typically gets called
  145.      * by the AWT to handle painting this component. It paints this component
  146.      * using the given graphics context. The graphics context clipping region
  147.      * is set to the bounding rectangle of this component and its <0,0>
  148.      * coordinate is this component's top-left corner.
  149.      *
  150.      * The horizontal position of the image is shifted according to the current offset.
  151.      *
  152.      * @param g the graphics context used for painting
  153.      * @see java.awt.Component#repaint
  154.      * @see java.awt.Component#update
  155.      */
  156.     public void paint(Graphics g)
  157.     {
  158.         if ((images != null) && (loopslot < images.size()))
  159.         {
  160.             Image img = ((AnimatorImage)images.elementAt(loopslot)).image;
  161.             if (img != null)
  162.                 g.drawImage(img, curOffset, 0, this);
  163.         }
  164.     }
  165.  
  166. }
  167.  
  168.